#!/bin/bash
# 数据导入脚本

echo "开始导入南昌景点数据..."

# 检查Python环境
if ! command -v python3 &> /dev/null; then
    echo "错误: 未找到Python3"
    exit 1
fi

# 检查Django项目
if [ ! -f "backend/manage.py" ]; then
    echo "错误: 未找到Django项目"
    exit 1
fi

cd backend

# 运行数据导入脚本
python3 manage.py shell << EOF
import json
import os
from apps.attractions.models import Attraction

# 读取JSON数据
json_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'database/mock_data/nanchang_attractions.json')
with open(json_path, 'r', encoding='utf-8') as f:
    attractions_data = json.load(f)

# 导入数据
count = 0
for data in attractions_data:
    attraction, created = Attraction.objects.get_or_create(
        name=data['name'],
        defaults={
            'latitude': data['latitude'],
            'longitude': data['longitude'],
            'ticket_price': data['ticket_price'],
            'visit_time': data['visit_time'],
            'popularity': data['popularity'],
            'description': data.get('description', ''),
            'address': data.get('address', '')
        }
    )
    if created:
        count += 1
        print(f"导入景点: {attraction.name}")

print(f"\n共导入 {count} 个景点")
EOF

echo "数据导入完成！"

